import React, { Component } from 'react';

import './index.less';

import {
    toast,
    ajax,
    base,
    high
} from 'nc-lightapp-front';

import AdjustSort from '../AdjustSort';

import HROrgReger from '../../../../../hrpub/common/components/referSearch/org';

import language from '../../language';
import setQueryMeta from "../../request-functions/setQueryMeta";

import BackBtn from '../../../../../hrpub/common/components/hr-back';
import getSysConf from "../../container/functions/getSysConf";

const {
    Refer
} = high;

const {
    NCCheckbox
} = base;

class Header extends Component {
    constructor(props) {
        super(props);

        this.adjustSort = new AdjustSort(props);

        this.handleHumanOrgTreeChange = this.handleHumanOrgTreeChange.bind(this);
        this.buttonClickHandle = this.buttonClickHandle.bind(this);
        this.onChangeHandle = this.onChangeHandle.bind(this);
    }

    handleHumanOrgTreeChange(value) {
        this.props.updateState({
            humanResourceOrganizationValue: value
        }, async () => {
            await this.getSysConf(value);
            if (this.props.hasSearched) {
                this.props.getMainTableData();
            }
            if (value) {
                this.props.setButtonStatus2('mainOrg');
            }
        });

    }

    getSysConf = (orgValue) => {
        return getSysConf.call(this, orgValue);
    };

    buttonClickHandle(props, btnCode) {
        const { page } = this.props;

        // 新增
        if (btnCode === 'Add') {
            this.props.updateState({
                mainPageButtonClick: 'add'
            });
            this.props.toEditPage('add');
        }
        // 查询
        else if (btnCode === 'Query') {
            // this.props.updateState({
            //     searchModalVisible: true
            // });
            setQueryMeta.call(this);
            this.props.search.openAdvSearch('ncc600704query', true);
        }
        // 刷新
        else if (btnCode === 'Refresh') {
            this.props.getMainTableData();
        }
        // 排序
        else if (btnCode === 'Sort') {
            this.props.updateState({
                sortModalVisible: true
            });
        }
        // 批量删除
        else if (btnCode === 'BatchDelete') {
            this.props.deleteMainTableRow();
        }
        // 调整顺序
        else if (btnCode === 'AdjustSort') {
            this.adjustSort.showAdjustPage();
        }
        // 取消
        else if (btnCode === 'Cancel' && page === 'adjust-sort') {
            this.adjustSort.closeAdjustPage();
        }
        // 保存
        else if (btnCode === 'Save' && page === 'adjust-sort') {
            this.adjustSort.saveShowOrder();
        }
        // 转档
        else if (btnCode === 'SwitchToDoc') {
            this.props.updateState({
                switchToDocModalVisible: true
            });
        }
        // 转人才库
        else if (btnCode === 'ZRCK') {
            let rows = this.props.editTable.getCheckedRows('ncc600704list');
            if(rows==null || rows.length<=0){
                toast({ color: 'warning', content: '请至少选中一条数据' });
                return;
            }
             
            let pks=[];
            rows.map((item) => {
                pks.push(item.data.values.pk_psndoc.value);
            });
            this.props.updateState({
                pks:pks,
                showZRCKModal: true
            });
            
           
        }
        // 转入职登记
        else if (btnCode === 'ZRZDJ') {
            
            let rows = this.props.editTable.getCheckedRows('ncc600704list');
            if(rows==null || rows.length<=0){
                toast({ color: 'warning', content: '请至少选中一条数据' });
                return;
            }
             
            let pks=[];
            rows.map((item) => {
                pks.push(item.data.values.pk_psndoc.value);
            });
            ajax({
                url: '/nccloud/hrhi/psndoc/ZRZDJAction.do',
                data: {
                    pks:pks
                 },
                success: (res) => {
                    this.props.getMainTableData();
                    toast({ color: 'success', content: '转入入职登记成功' });
                }
            });
        }
        // 批量修改
        else if (btnCode === 'BatchEdit') {
            this.props.getRef('batchEdit').getBatchTemplateData();
        }
        // 批量添加子集
        else if (btnCode === 'BatchAddSub') {
            this.props.batchAddSub();
        }
        // 联查花名册
        else if (btnCode === 'ListReport') {
            this.props.updateState({
                rosterModalVisible: true
            });
        }
        // 打印
        else if (btnCode === 'Print') {
            this.props.print();
        }
        // 附件管理
        else if (btnCode === 'File') {
            this.props.fileManager()
        }
        // 输出
        else if (btnCode === 'output') {
            this.props.printOutput();
        }
        // 生成入职申请
        else if (btnCode === 'entryapply') {
            this.props.entryApply();
        }
        // 联查卡片
        else if (btnCode === 'cardrpt') {
            this.props.relativeQueryCard();
        }// 预览电子签
        else if (btnCode === 'YLDZQ') {
            let rows = this.props.editTable.getCheckedRows('ncc600704list');
            if (rows.length != 1) {
                toast({ color: 'warning', content: '请选中一条数据' });
                return;
            }
            let pk_psndoc = rows[0].data.values.pk_psndoc.value;
            let pk_psnjob = rows[0].data.values.pk_psnjob.value;
            let contractId = rows[0].data.values.jobglbdef62 && rows[0].data.values.jobglbdef62.value ?rows[0].data.values.jobglbdef62.value :"";
            ajax({
                url: '/nccloud/hrhi/psndoc/YLDZQAction.do',
                data: {
                    pk_psndoc: pk_psndoc,
                    pk_psnjob: pk_psnjob,
                    contractId:contractId,
                 },
                success: (res) => {
                    window.open(res.data.url,"_blank");
                }
            });
        } //签署电子签
        else if (btnCode === 'SendDZQ') {
            let rows = this.props.editTable.getCheckedRows('ncc600704list');
            if (rows.length != 1) {
                toast({ color: 'warning', content: '请选中一条数据' });
                return;
            }
            let pk_psndoc = rows[0].data.values.pk_psndoc.value;
            let pk_psnjob = rows[0].data.values.pk_psnjob.value;
            let contractId = rows[0].data.values.jobglbdef62 && rows[0].data.values.jobglbdef62.value ?rows[0].data.values.jobglbdef62.value :"";

            ajax({
                url: '/nccloud/hrhi/psndoc/SendDZQAction.do',
                data: {
                    pk_psndoc: pk_psndoc,
                    pk_psnjob: pk_psnjob,
                    contractId:contractId,
                 },
                success: (res) => {
                    toast({ color: 'success', content: '发送电子签平台成功' });
                }
            });

        }
        else if (btnCode === 'ImportData') {
            this.props.updateState({
                importModalVisible: true
            })
        }
    }

    onChangeHandle(type) {
        return (value) => {
            let state = {
                [type]: value
            };

            if (type === 'show_reject_offer' && value) {
                state['include_child_orgs'] = false;
                state['include_child_depts'] = false;
                state['show_refuse_entry'] = false;
                state['include_entry_apply'] = false;
            }
            if (type === 'show_refuse_entry' && value) {
                state['include_child_orgs'] = false;
                state['include_child_depts'] = false;
                state['show_reject_offer'] = false;
                state['include_entry_apply'] = false;
            }
            if ((type === 'include_child_orgs' || type === 'include_child_depts'
                || type === 'include_entry_apply')
                && value) {
                state['show_reject_offer'] = false;
                state['show_refuse_entry'] = false;
            }

            this.props.updateState(state, () => {
                if (this.props.hasSearched) {
                    this.props.getMainTableData();
                }
            });
        }
    }

    render() {
        const {
            humanResourceOrganizationValue,
            button,
            page,
            include_child_depts,
            include_child_orgs,
            meta,
            show_reject_offer,
            show_refuse_entry,
            include_entry_apply
        } = this.props;

        let tmp = meta.getMeta() || true;

        if (typeof tmp === 'object' && Object.keys(tmp).length <= 0) {
            tmp = false;
        }

        return (
            <div className='multiple-header'>
                <div className="header">
                    {
                        page === 'adjust-sort' &&
                        <BackBtn
                            title={language['hi6007-000012']/* 国际化处理： 返回*/}
                            onClick={() => {
                                this.adjustSort.closeAdjustPage();
                            }}

                        />
                    }
                    {
                        page === 'main' &&
                        <div className="header-refer">
                            <If condition={tmp}>
                                <HROrgReger
                                    getOrgData={this.handleHumanOrgTreeChange}
                                    orgVal={humanResourceOrganizationValue}
                                />
                            </If>
                        </div>
                    }
                    {
                        humanResourceOrganizationValue.refpk &&
                        <div className="btn-group">
                            <div className="definition-icons">
                                {button.createButtonApp({
                                    area: 'head',
                                    onButtonClick: this.buttonClickHandle
                                })}
                            </div>
                        </div>
                    }
                </div>
                {
                    page === 'main' && humanResourceOrganizationValue.refpk &&
                    <div className="header-checkbox-group">
                        <NCCheckbox
                            className="header-checkbox"
                            color="primary"
                            onChange={this.onChangeHandle('include_child_orgs')}
                            checked={include_child_orgs}
                        >
                            {language['hi6007-000045']}{/* 国际化处理： 包含下级组织人员*/}
                        </NCCheckbox>
                        <NCCheckbox
                            className="header-checkbox"
                            color="primary"
                            onChange={this.onChangeHandle('include_child_depts')}
                            checked={include_child_depts}
                        >
                            {language['hi6007-000046']}{/* 国际化处理： 包含下级部门人员*/}
                        </NCCheckbox>
                        <NCCheckbox
                            className="header-checkbox"
                            color="primary"
                            onChange={this.onChangeHandle('show_reject_offer')}
                            checked={show_reject_offer}
                        >
                            {language['hi6007-000297']}{/* 国际化处理： 显示拒绝offer人员*/}
                        </NCCheckbox>
                        <NCCheckbox
                            className="header-checkbox"
                            color="primary"
                            onChange={this.onChangeHandle('show_refuse_entry')}
                            checked={show_refuse_entry}
                        >
                            {language['hi6007-000298']}{/* 国际化处理： 显示拒绝入职人员*/}
                        </NCCheckbox>
                        <NCCheckbox
                            className="header-checkbox"
                            color="primary"
                            onChange={this.onChangeHandle('include_entry_apply')}
                            checked={include_entry_apply}
                        >
                            {language['hi6007-000299']}{/* 国际化处理： 包含已提交入职申请人员*/}
                        </NCCheckbox>
                    </div>
                }
            </div>
        );
    }
}

export default Header;
